﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Timers;

using ScrumTable.Common;

#endregion

namespace ScrumTable.DL.Data.Generic.Services
{
    /// <summary>
    /// This class is used to perform timed events in the main message loop.
    /// </summary>
    internal class DataSaveTimer : DisposeBase
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private const int DataSaveInterval = 2000; // in ms

        private volatile bool _enabled = false;

        private readonly Timer _timer;
        private readonly Action _callback;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new DataSaveTimer instance, no public constructor
        /// needed.
        /// </summary>
        internal DataSaveTimer(Action callback)
        {
            PreCondition.AssertNotNull(callback, "callback");

            _callback = callback;
            _timer = new Timer(DataSaveInterval);
            _timer.AutoReset = false;
            _timer.Elapsed += TimerCallback;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Starts the wait interval to the next timed event.
        /// </summary>
        public void Start()
        {
            _enabled = true;
            _timer.Start();
        }

        /// <summary>
        /// Releases managed (c# or .net) resources.
        /// </summary>
        protected override void ReleaseManagedResources()
        {
            _enabled = false;
            _timer.Stop();
            _timer.Elapsed -= TimerCallback;
            _timer.Dispose();
        }

        private void TimerCallback(object sender, ElapsedEventArgs e)
        {
            if (_enabled)
                _callback();
        }


        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}